package com.ddm.service.impl.vxImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddm.constant.MessageConstant;
import com.ddm.dto.page.PageQueryDTO;
import com.ddm.dto.vxDTO.QuestionAnalysis;
import com.ddm.dto.vxDTO.QuestionDTO;
import com.ddm.entity.Standard;
import com.ddm.entity.vxEntity.Question;
import com.ddm.entity.vxEntity.User;
import com.ddm.enumeration.QuestionType;
import com.ddm.exception.DataErrorException;
import com.ddm.mapper.QuetsionsMapper;
import com.ddm.result.PageResult;
import com.ddm.service.StandardService;
import com.ddm.service.vxService.QuetsionsService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.QuestionVO;
import com.ddm.vo.vxVo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author horizon
 * @since 2025-01-17
 */
@Service
public class QuetsionsServiceImpl extends ServiceImpl<QuetsionsMapper, Question> implements QuetsionsService {

    @Autowired
    QuetsionsService quetsionsService;
    @Autowired
    StandardService standardService;
    @Autowired
    QuetsionsMapper quetsionsMapper;
    /**
     * 分页查询试题
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult getQuestionList(PageQueryDTO pageQueryDTO) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        Page<Question> questionPage = new Page(pageQueryDTO.getPage() , pageQueryDTO.getPageSize());
        queryWrapper.orderByAsc(Question::getQuestionScore);
        List<Question> records = quetsionsService.page(questionPage, queryWrapper).getRecords();
        long total = questionPage.getTotal();
        List<QuestionVO> questionListVos = BeanCopyUtil.BeanCopyList(records, QuestionVO.class);
        return new PageResult(total,questionListVos);
    }


    @Override
    public PageResult getSQList(PageQueryDTO pageQueryDTO) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        Page<Question> questionPage = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        wrapper.eq(Question::getQuestionType , QuestionType.SINGLE_CHOICE);
        List<Question> records = quetsionsService.page(questionPage, wrapper).getRecords();
        long total = questionPage.getTotal();
        List<QuestionVO> questionVOS = BeanCopyUtil.BeanCopyList(records, QuestionVO.class);
        return new PageResult(total,questionVOS);
    }

    @Override
    public PageResult getMQList(PageQueryDTO pageQueryDTO) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        Page<Question> questionPage = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        wrapper.eq(Question::getQuestionType , QuestionType.MULTIPLE_CHOICE);
        List<Question> records = quetsionsService.page(questionPage, wrapper).getRecords();
        long total = questionPage.getTotal();
        List<QuestionVO> questionVOS = BeanCopyUtil.BeanCopyList(records, QuestionVO.class);
        return new PageResult(total,questionVOS);
    }

    @Override
    public List<QuestionVO> getStandardQuestionList(Long id) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Question::getStandardId,id);
        List<Question> questions = quetsionsMapper.selectList(wrapper);
        if (questions == null){
            throw new DataErrorException(MessageConstant.ERROR_DATA);
        }
        List<QuestionVO> questionVOS = BeanCopyUtil.BeanCopyList(questions, QuestionVO.class);
        return questionVOS;
    }

    @Override
    public void saveQuestion(Question question) {
        Standard standard = standardService.getById(question.getStandardId());
        if (standard == null){
            //没有相关联的标准表
            throw new DataErrorException(MessageConstant.ERROR_DATA);
        }
        quetsionsService.save(question);
    }


}
